package com.wwy.arithmetic.suanfa;

import java.util.*;

/**
 * 数组中的两束之和
 */
public class LiangShuZhiHe {

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 6, 10};
        int[] target = getTarget(a, 16);
        for (int i : target) {
            System.out.println("i = " + i);
        }
    }

    static int[] getTarget(int[] nums, int target) {
        int[] res = new int[2];
        if (nums == null || nums.length == 0) {
            return res;
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                res[1] = i;
                res[0] = map.get(temp);
            }
            map.put(nums[i], i);
        }
        return res;
    }
}
